package service

import (
	"log"
	"prometheus_import_consul/global"
	"prometheus_import_consul/tools"
)

type ServicesMap map[string]map[string][]interface{}

func ServiceTCP() {

	// 解析JSON内容到结构体
	var results ServicesMap
	err := tools.ReadJSONFile(global.Config.JsonFile.Servicejson_Path, &results)
	if err != nil {
		log.Fatalf("Error reading adn parsing JSON file: %s,error: %v\n", global.Config.JsonFile.Servicejson_Path, err)
	}
	log.Printf("Success reading and parsing JSON file: %s\n", global.Config.JsonFile.Servicejson_Path)

	// 遍历解析结果并添加服务
	for ip, services := range results {
		for name, infoArray := range services {
			svcType := infoArray[0].(string)
			port := infoArray[1].(string) // JSON numbers are floats
			env := infoArray[2].(string)
			businessCode := infoArray[3].(string)
			tools.Add_Service(ip, name, svcType, port, env, businessCode)
		}
	}
}
